import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class editMemberDialog extends JDialog{
	
	// GUI
		private JLabel nameLabel;
		private JTextField nameTextField;
		private JLabel memberIdLabel;
		private JTextField memberIdTextField;
		private JLabel phoneLabel;
		private JTextField phoneTextField;
		private JLabel ageLabel;
		private JTextField ageTextField;
		private JLabel genderLabel;
		private JTextField genderTextField;
		private JLabel stateLabel;
		private ButtonGroup stateGroup;
		private JRadioButton onlineRadiobutton;
		private JRadioButton offlineRadiobutton;
		private JLabel idTypeLabel;
		private JTextField idTextField;
		private JLabel timeLabel;
		private String startTime;
		private JLabel startTimeLabel;
		private JButton editButton;
		private JButton cancelButton;
		
		// event handles
		private StateHandler stateHandler;
		private EditHandler editHandler;
		private CancelHandler CancelHandler;
		
		// data control
		private memberManager mm;
		private membershipFrame mf;
		private member m;
		
		public editMemberDialog(membershipFrame initMf, 
				memberManager initMm, String memberName){
			mf = initMf;
			mm = initMm;
			m = mm.findMemberByName(memberName);
			layoutGUI();
		}
		
		private void layoutGUI() {
			// TODO Auto-generated method stub
			this.setTitle("Edit Member Info");
			this.setResizable(false);
			nameLabel = new JLabel("Name: ");
			nameTextField = new JTextField(m.getName());
			nameTextField.setEnabled(false);
			
			memberIdLabel = new JLabel("Member ID: ");
			memberIdTextField = new JTextField(""+mm.getMemberId());
			memberIdTextField.setEnabled(false);
			
			phoneLabel = new JLabel("Phone: ");
			phoneTextField = new JTextField(""+m.getPhone());
			
			ageLabel = new JLabel("Age: ");
			ageTextField = new JTextField(m.getAge());
			
			genderLabel = new JLabel("Gender: ");
			genderTextField = new JTextField(m.getGender());
			genderTextField.setEnabled(false);
			
			stateLabel = new JLabel("Current State: ");
			stateGroup = new ButtonGroup();
			onlineRadiobutton = new JRadioButton("On ");
			offlineRadiobutton = new JRadioButton("Off ");
			stateGroup.add(onlineRadiobutton);
			stateGroup.add(offlineRadiobutton);
			
			idTypeLabel = new JLabel(m.getIdType());
			idTextField = new JTextField(m.getId());
			idTextField.setEnabled(false);

			timeLabel = new JLabel("From: ");
			startTimeLabel = new JLabel(m.getStartTime());
			
			editButton = new JButton("Finish");
			cancelButton = new JButton("Cancel");
			
			// add handlers
			stateHandler = new StateHandler();
			editHandler = new EditHandler();
			CancelHandler = new CancelHandler();
			
			onlineRadiobutton.addActionListener(stateHandler);
			offlineRadiobutton.addActionListener(stateHandler);
			editButton.addActionListener(editHandler);
			cancelButton.addActionListener(CancelHandler);
			
			GridBagLayout layout = new GridBagLayout();
			this.setLayout(layout);
			addComponent(nameLabel,				this,	0,	0,	1,	1);
			addComponent(nameTextField,			this,	1,	0,	3,	1);
			addComponent(memberIdLabel,			this,	0,	1,	1,	1);
			addComponent(memberIdTextField,		this,	1,	1,	3,	1);
			addComponent(phoneLabel,			this,	0,	2,	1,	1);
			addComponent(phoneTextField,		this,	1,	2,	3,	1);
			addComponent(ageLabel,				this,	0,	3,	1,	1);
			addComponent(ageTextField,			this,	1,	3,	1,	1);
			addComponent(genderLabel,			this,	2,	3,	1,	1);
			addComponent(genderTextField,		this,	3,	3,	1,	1);	
			addComponent(stateLabel,			this,	0,	4,	1,	1);
			addComponent(onlineRadiobutton,		this,	1,	4,	1,	1);
			addComponent(offlineRadiobutton,	this,	2,	4,	1,	1);
			addComponent(idTypeLabel,			this,	0,	5,	1,	1);
			addComponent(idTextField,			this,	1,	5,	3,	1);
			addComponent(timeLabel,				this,	0,	6,	1,	1);
			addComponent(startTimeLabel,		this,	1,	6,	2,	1);
			addComponent(editButton,			this,	2,	7,	1,	1);
			addComponent(cancelButton,			this,	3,	7,	1,	1);
			
			// size and locate the dialog at the center of the screen
			this.pack();
			this.setModal(true);
			int w = getWidth();
			int h = getHeight();
			Toolkit tk = Toolkit.getDefaultToolkit();
			Dimension screenSize = tk.getScreenSize();
			int screenWidth = screenSize.width;
			int screenHeight = screenSize.height;
			int x = (screenWidth - w)/2;
			int y = (screenHeight - h)/2;
			setLocation(x,y);
		}
		
		// method helping GridBagLayout
		public void addComponent(	JComponent jc, Container c, 
				int x, int y, int w, int h){
			GridBagConstraints gbc = new GridBagConstraints();
			gbc.gridx = x;
			gbc.gridy = y;
			gbc.gridwidth = w;
			gbc.gridheight = h;
			gbc.fill = GridBagConstraints.HORIZONTAL;
			gbc.insets = new Insets(5, 5, 5, 5);
			c.add(jc, gbc);
		}
		
		class StateHandler implements ActionListener{

			@Override
			public void actionPerformed(ActionEvent ae) {
				// TODO Auto-generated method stub
				String label = ((AbstractButton) ae.getSource()).getText();
				m.setCurrentState(label);
			}
			
		}
		
		class EditHandler implements ActionListener{

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				try{
					m.setPhone(Integer.parseInt(phoneTextField.getText()));
					m.setAge(Integer.parseInt(ageTextField.getText()));
				}
				catch(NumberFormatException nfe){
					JOptionPane.showMessageDialog(editMemberDialog.this, "Error: Invalid input", "Error", JOptionPane.ERROR_MESSAGE);
				}
				// update
				mm.update(m);
				mf.editMember(m);
				System.out.println("Edit Member: \n"+m.toString());
				editMemberDialog.this.setVisible(false);
			}
			
		}
		
		class CancelHandler implements ActionListener{

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				editMemberDialog.this.setVisible(false);
			}
			
		}
}
